/**
 * @copyright Copyright (c) 2025 
 * CSG Digital Grid Technology (Guangdong) Co., Ltd.
 * @author xxxcsg
 * @brief otp读取工具
 */

#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include "ohos_types.h"
#include "common.h"

// const static uint8_t ree_root_hash[] = {   0x93,0xA3,0xF1,0x34,0x1B,0xE8,0xD6,0x0B,
//                                             0x74,0xC1,0xDF,0x56,0x08,0xAB,0xB1,0x4E,
//                                             0x18,0x46,0x13,0xA3,0x7D,0xD2,0x47,0x07,
//                                             0x9B,0x84,0xD5,0x07,0x30,0x6F,0x19,0x98     };

const static uint8_t ree_root_hash[] = {   0x34,0xF1,0xA3,0x93,0x0B,0xD6,0xE8,0x1B,
                                            0x56,0xDF,0xC1,0x74,0x4E,0xB1,0xAB,0x08,
                                            0xA3,0x13,0x46,0x18,0x07,0x47,0xD2,0x7D,
                                            0x07,0xD5,0x84,0x9B,0x98,0x19,0x6F,0x30     };


static int read_otp_byte_data(uint32_t offset, uint8_t *data, uint32_t length)
{
    int ret = 0;
    for (uint8_t i = 0; i < length; i++) {
        ret = ot_mpi_otp_read_byte(offset + i, &data[i]);
        if (ret != 0) {
            printf("read otp byte data fail\n");
            return ret;
        }
        // printf("otp_byte_data: 0x%x \n", data[i]);
    }
}

static int read_otp_word_data(uint32_t offset, uint8_t *data, uint32_t length)
{
    uint32_t otp_word_data = 0;
    uint8_t len = 0;
    int ret = 0;

    length = length / 4;
    for (uint8_t i = 0; i <= length; i++) {
        uint32_t tmp_valuie = 0;
        len = i * 4;
        ret = ot_mpi_otp_read_word(offset + len, &otp_word_data);
        if (ret != 0) {
            printf("read otp word data fail\n");
            return ret;
        }
        // printf("value : %x %x ",otp_word_data,tmp_valuie);
        // memcpy(data + len, &tmp_valuie, 4);

        data[len] = (otp_word_data >> 24) & 0xff;
        data[len + 1] = (otp_word_data >> 16) & 0xff;
        data[len + 2] = (otp_word_data >> 8) & 0xff;
        data[len + 3] = otp_word_data & 0xff;
    }
    

    return 0;
}


int main(int argc, char** argv)
{
    if(argc == 2)
    {
        if(!strcmp(argv[1],"-v"))
        {
            printf_func_version(HAWK_EYE_FUNC_OTP_SERVER);
        }
        return 0;
    }
    
    uint8_t ree_hash_data[32] = {0};
    uint8_t otp_byte_data = 0;

	// printf ("otp application start\n");

    ot_mpi_otp_init(); //otp模块初始化

    read_otp_word_data(0x60, &ree_hash_data, 32);
    // printf("otp_value : ");
    // for (uint8_t i = 0; i < 32; i++) {
    //     printf("0x%x ", ree_hash_data[i]);
    // }
    // printf("\r\n");
    if(memcmp(ree_hash_data, ree_root_hash, 32))
    {
        printf("otp no pass\n");
        exit(0);
    }
    read_otp_byte_data(0x0, &otp_byte_data, 1);
    if(otp_byte_data != 0x07)
    {
        printf("otp no pass\n");
        exit(0);
    }
    read_otp_byte_data(0x16, &otp_byte_data, 1);
    if(otp_byte_data != 0xff)
    {
        printf("otp no pass\n");
        exit(0);
    }
    printf("otp pass\n");
    // ot_mpi_otp_deinit();
    // printf("otp application end\n");
    // while (1) {
    //     pause();
    // }
    exit(0);
    // return 0;
}